package org.abratuhi.kvngroupware.login.mptua;

import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageProceeder;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.team.model.Team;
import org.abratuhi.kvngroupware.user.manager.UserManager;
import org.abratuhi.kvngroupware.user.model.User;
import org.abratuhi.kvngroupware.widget.WidgetManager;
import org.jdom.Element;

public class LoginResponseMessageProceeder implements MessageProceeder{

	MessageSender sender;
	LoginManager loginmanager;
	UserManager usermanager;
	WidgetManager widgetmanager;

	public LoginResponseMessageProceeder(MessageSender sender, LoginManager loginmanager, UserManager usermanager, WidgetManager widgetmanager){
		this.sender = sender;
		this.loginmanager = loginmanager;
		this.usermanager = usermanager;
		this.widgetmanager = widgetmanager;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element response = ((Element) message.getRoot().getChildren("response").get(0));
		String type = response.getAttributeValue("type");
		String action = response.getAttributeValue("action");
		String status = response.getAttributeValue("status");
		User u = response.getChild("user")!=null ? User.fromElement((Element) response.getChild("user")) : null;

		if(type.equals("user")){
			if(status.equals("ok")){
				if(action.equals("login")){
					if(u.getId().equals(loginmanager.getUser().getId())){
						// update local user info
						loginmanager.setUser(u);
						loginmanager.setTeam(new Team(u.getTeamId()));
						usermanager.put(u.getId(), u);

						// request teams
						sender.sendMessage(sender.createMessage("type,action", "team,get", "", new Element[0]));

						// request users
						sender.sendMessage(sender.createMessage("type,action","user,get", "", new Element[]{new User().toElement()}));

						// request brainstorms
						sender.sendMessage(sender.createMessage(
								"type,action,brainstormId,userId,teamId,brainstormTheme",
								"brainstorm,get,*,"+loginmanager.getUser().getId()+",*,*",
								"",
								new Element[0]));

						// request chatrooms
						sender.sendMessage(sender.createMessage(
								"type,action,chatroomId,userId,teamId,chatroomTheme",
								"chatroom,get,*,*,*,*",
								"",
								new Element[0]));

						// request scenarios
						sender.sendMessage(sender.createMessage(
								"type,action,userId,teamId,scenarioId",
								"scenario,get,*,"+loginmanager.getTeam().getId()+",*",
								"",
								new Element[0]));

						// request jokes
						sender.sendMessage(sender.createMessage(
								"type,action,teamId,userId,jokeId",
								"joke,get,"+loginmanager.getTeam().getId()+",*,*",
								"",
								new Element[0]));
						
						// dispose login window
						widgetmanager.get("login").deleteWindow();
					}
					else{
						usermanager.put(u.getId(), u);
					}
				}
				else if(action.equals("logout")){
					if(u.getId().equals(loginmanager.getUser().getId())){
						// TODO ?
					}
					else{
						usermanager.remove(u.getId());
					}
				}
			}
		}
	}

}
